import {login, getUserInfo, logout} from '@/api/login'

export default {
    namespaced: true,//开启命名空间
    // 局部状态
    state: {
        changeShow: false,
    },
    // 局部读取
    getters: {
        changeShow: state => state.changeShow,
    },
    // 局部变化:命名规则：动词_名词，全部大写
    mutations: {
        setChangeShow(state, payload) {
            state.changeShow = payload;
        },
    },
    // 局部动作
    actions: {
        // 登录
        login({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo).then(res => {
                    resolve(res.result)
                }).catch(error => {
                    console.log(error);
                    reject(error)
                })
            })
        },
        // 获取用户信息
        getUserInfo({commit}) {
            return new Promise((resolve, reject) => {
                getUserInfo().then(res => {
                    const user = res.result
                    const avatar = user.avatar == "" ? require("@/assets/image/avatar.jpg") : user.avatar
                    // setUserInfo(res.result, commit)
                    // commit('SET_AVATAR', avatar)
                    resolve(res.result)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 登出
        logout({commit}) {
            return new Promise((resolve, reject) => {
                logout().then(res => {
                    // logOut(commit)
                    resolve()
                }).catch(error => {
                    // logOut(commit)
                    reject(error)
                })
            })
        }
    }
};
